package net.sk.china.front.strategy;

import net.sk.china.front.config.LoginTypeConfig;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 登录工厂类
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/6/4 14:49
 * // +----------------------------------------------------------------------
 */
@Component
public class UserLoginFactory implements ApplicationContextAware {

    private final static Map<String, UserInterface> granterPool = new ConcurrentHashMap<>();

    private final LoginTypeConfig loginTypeConfig;


    public UserLoginFactory(LoginTypeConfig loginTypeConfig) {
        this.loginTypeConfig = loginTypeConfig;
    }

    /**
     * 从配置文件中读取策略信息存储到map中
     * @param applicationContext 上下文
     * @throws BeansException 异常
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        loginTypeConfig.getTypes().forEach((k, y) -> granterPool.put(k, (UserInterface) applicationContext.getBean(y)));
    }

    /**
     * 对外提供获取具体策略
     * @param type 用户的登录方式，需要跟配置文件中匹配
     * @return {@code UserInterface}
     */
    public UserInterface getInterface(String type) { return granterPool.get(type); }
}
